package enumdemo;

public enum TestEnum {

    //当我们定义好一个枚举类以后
    //这个枚举类，默认继承 Enum 这个类
    //枚举的构造方法默然是私有的
    RED(1,"红色"),BLACK(1,"红色"),
    GREEN(1,"红色"),WHITE(1,"红色");

    public String color;
    public int ordinal;

    //系统默认提供不带参数的构造方法
    //当我们自己编写了带参数的构造方法，则所有对象都需要实例化
    private TestEnum(int ordinal,String color){
        this.ordinal = ordinal;
        this.color = color;
    }

    public static void main(String[] args) {

        //values ：以数组形式返回枚举类型的所有成员
        TestEnum[] testEnums = TestEnum.values();
        for (int i = 0; i < testEnums.length; i++) {
            System.out.print(testEnums[i] + " ");
        }
        System.out.println();
        System.out.println("======================");

        //ordinal ：获取枚举成员的索引位置
        for (int i = 0; i < testEnums.length; i++) {
            System.out.print(testEnums[i].ordinal() + "     ");
        }

        System.out.println();
        System.out.println("======================");

        //valueOf ：将普通字符串转换为枚举实例
        TestEnum val1 = TestEnum.valueOf("RED");
//        TestEnum val2 = TestEnum.valueOf("RED2");
        System.out.println(val1);
//        System.out.println(val2);

        //compareTo ：比较两个枚举成员在定义时的顺序
        System.out.println(BLACK.compareTo(WHITE));
        System.out.println(BLACK.compareTo(GREEN));
        System.out.println(WHITE.compareTo(RED));

    }



    public static void main1(String[] args) {
        TestEnum testEnum = RED;
        switch (testEnum){
            case RED :
                System.out.println("RED");
                break;
            case BLACK :
                System.out.println("BLACK");
                break;
            case GREEN :
                System.out.println("GREEN");
                break;
            case WHITE :
                System.out.println("WHITE");
                break;
            default:
                System.out.println("颜色错误");
                break;
        }
    }
}
